﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using TiEntityObject.Transport;

namespace TI_EntityLib.TiEntityObjects.DayLog
{
    /// <summary>
    /// Информация о закачке реплики в БД
    /// </summary>
    public class DayLogTransport
    {
        public DayLogTransport()
        {
            this.TransportFileList = new List<TransportFile>();
            this.TransportSessions = new List<DayLogTransportSession>();
        }

        public void AddTransportFiles(List<TransportFile> transportList)
        {
            Func<TransportFile, bool> addItem = delegate(TransportFile file)
            {
                foreach (TransportFile i in TransportFileList)
                {
                    if (i.FileName.Equals(file.FileName))
                        return true;
                }
                TransportFileList.Add(file);
                return true;
            };
            transportList.All(addItem);
        }
      
        /// <summary>
        /// Проверить наличие танспортного файла если есть убрать
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="fileName"></param>
        private void RemoveExists(TransportFile file)
        {
            TransportFile tf=GetTransportFile(file.Name);
            if (tf != null)
                this.TransportFileList.Remove(tf); 
        }

        public void AddTransportFiles(string folder,  string[]  files)
        {
            TransportFile tf=null;
            foreach (string i in files)
           {
               tf = new TransportFile(folder, i);
               RemoveExists(tf);
               TransportFileList.Add(tf);
           }
        }

        private string RemoveFileExctention(string filename)
        {
            filename = filename.Replace(".xml", string.Empty);  
            FileInfo fi = new FileInfo(filename);
            if (fi.Extension.Length > 0)
                return fi.Name.Replace(fi.Extension, string.Empty);
            else
                return fi.Name; 
        }
        
        /// <summary>
        /// по наименованию файла вернуть TransportFile
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public TransportFile GetTransportFile(string fileName)
        {
            fileName = RemoveFileExctention(fileName);
            foreach (TransportFile i in this.TransportFileList)
            {
                if (i.Name == fileName)
                    return i;
            }
            return null;
        }
        
        /// <summary>
        /// Пройтись по файлам -вытащить количество имеющее статус  TransportFileStatus
        /// </summary>
        public int ProcessedFile { get; set; }

        public List<TransportFile> TransportFileList { get; set; }

        public List<DayLogTransportSession> TransportSessions { get; set; }

        #region Статистика по транспорту
        //DownloadOk, UnzipOk, UnzipErr, ProcessedOk, ProcessedErr 
        public int DownloadOk { get { return 1000; } }

        public int Zip { get { return this.TransportFileList.Count ; } }

        public int UnzipOk { get { return GetCountByStatus( TransportFileStatusName.UnzipOk) ; } }

        public int UnzipErr { get { return GetCountByStatus(TransportFileStatusName.UnzipErr); } }

        public int Processed { get { return ProcessedOk + ProcessedErr; } }

        public int ProcessedOk { get { return GetCountByStatus(TransportFileStatusName.ProcessedOk); } }

        public int ProcessedErr { get { return GetCountByStatus(TransportFileStatusName.ProcessedErr); } }

        private int GetCountByStatus(TransportFileStatusName status) {
            int r=0;
            foreach (TransportFile i in this.TransportFileList)
            {
                    if(i.ImInstatus (status )!=null)
                        ++r;
            }
            return r;
        }
        #endregion

    }

    public enum DayLogTransportSessionType{Download, DataProcessed}
   
    public class DayLogTransportSession{
        public long Start { get; set; }
        public long End { get; set; }
        public DayLogTransportSessionType SessionType { get; set; }
    }

}
